package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JingfeiJiezhishijian;
import com.ruoyi.system.service.IJingfeiJiezhishijianService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * jingfeijiezhishijianController
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Controller
@RequestMapping("/system/jiezhishijian")
public class JingfeiJiezhishijianController extends BaseController
{
    private String prefix = "system/jiezhishijian";

    @Autowired
    private IJingfeiJiezhishijianService jingfeiJiezhishijianService;

    @RequiresPermissions("system:jiezhishijian:view")
    @GetMapping()
    public String jiezhishijian()
    {
        return prefix + "/jiezhishijian";
    }

    /**
     * 查询jingfeijiezhishijian列表
     */
    @RequiresPermissions("system:jiezhishijian:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiJiezhishijian jingfeiJiezhishijian)
    {
        startPage();
        List<JingfeiJiezhishijian> list = jingfeiJiezhishijianService.selectJingfeiJiezhishijianList(jingfeiJiezhishijian);
        return getDataTable(list);
    }

    /**
     * 导出jingfeijiezhishijian列表
     */
    @RequiresPermissions("system:jiezhishijian:export")
    @Log(title = "jingfeijiezhishijian", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiJiezhishijian jingfeiJiezhishijian)
    {
        List<JingfeiJiezhishijian> list = jingfeiJiezhishijianService.selectJingfeiJiezhishijianList(jingfeiJiezhishijian);
        ExcelUtil<JingfeiJiezhishijian> util = new ExcelUtil<JingfeiJiezhishijian>(JingfeiJiezhishijian.class);
        return util.exportExcel(list, "jingfeijiezhishijian数据");
    }

    /**
     * 新增jingfeijiezhishijian
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存jingfeijiezhishijian
     */
    @RequiresPermissions("system:jiezhishijian:add")
    @Log(title = "jingfeijiezhishijian", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiJiezhishijian jingfeiJiezhishijian)
    {
        return toAjax(jingfeiJiezhishijianService.insertJingfeiJiezhishijian(jingfeiJiezhishijian));
    }

    /**
     * 修改jingfeijiezhishijian
     */
    @RequiresPermissions("system:jiezhishijian:edit")
    @GetMapping("/edit/{danweijiezhiid}")
    public String edit(@PathVariable("danweijiezhiid") String danweijiezhiid, ModelMap mmap)
    {
        JingfeiJiezhishijian jingfeiJiezhishijian = jingfeiJiezhishijianService.selectJingfeiJiezhishijianByDanweijiezhiid(danweijiezhiid);
        mmap.put("jingfeiJiezhishijian", jingfeiJiezhishijian);
        return prefix + "/edit";
    }

    /**
     * 修改保存jingfeijiezhishijian
     */
    @RequiresPermissions("system:jiezhishijian:edit")
    @Log(title = "jingfeijiezhishijian", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiJiezhishijian jingfeiJiezhishijian)
    {
        return toAjax(jingfeiJiezhishijianService.updateJingfeiJiezhishijian(jingfeiJiezhishijian));
    }

    /**
     * 删除jingfeijiezhishijian
     */
    @RequiresPermissions("system:jiezhishijian:remove")
    @Log(title = "jingfeijiezhishijian", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiJiezhishijianService.deleteJingfeiJiezhishijianByDanweijiezhiids(ids));
    }

    @PostMapping("findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("deptid")String ppDeptId, @RequestParam("jingfeipiciid")String ppJingfeipiciid){
        try {
            JingfeiJiezhishijian jingfeiJiezhishijian = new JingfeiJiezhishijian();
            jingfeiJiezhishijian.setDeptid(ppDeptId);
            jingfeiJiezhishijian.setJingfeipiciid(ppJingfeipiciid);

            List<JingfeiJiezhishijian> mmList = jingfeiJiezhishijianService.selectJingfeiJiezhishijianList(jingfeiJiezhishijian);

            if (mmList.size()==1) {
                jingfeiJiezhishijian = mmList.get(0);
                return JsonResult.getSuccessResult(jingfeiJiezhishijian);
            }else if (mmList.size()==0){
                return JsonResult.getSuccessResult(jingfeiJiezhishijian);
            }else{
                return JsonResult.getErrorResult("查询出错");
            }
        } catch (Exception e) {
            logger.error("jiezhishijian -> findone: "+e.getMessage());
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @PostMapping("shezhi")
    @ResponseBody
    public JsonResult<?> shezhi(@RequestParam("deptid")String ppDeptId,@RequestParam("jingfeipiciid")String ppJingfeipiciid,@RequestParam("jiezhishijian")String ppJiezhishijian){
        try {
            JingfeiJiezhishijian jingfeiJiezhishijian = new JingfeiJiezhishijian();
            jingfeiJiezhishijian.setDeptid(ppDeptId);
            jingfeiJiezhishijian.setJingfeipiciid(ppJingfeipiciid);

            SimpleDateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
            LocalDateTime ldt = dfm.parse(ppJiezhishijian).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusDays(1).minusSeconds(1);
            Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());

            List<JingfeiJiezhishijian> mmList = jingfeiJiezhishijianService.selectJingfeiJiezhishijianList(jingfeiJiezhishijian);

            if (mmList.size()==1) {
                jingfeiJiezhishijian = mmList.get(0);
                jingfeiJiezhishijian.setJiezhishijian(date);
                int modify = jingfeiJiezhishijianService.updateJingfeiJiezhishijian(jingfeiJiezhishijian);
                return modify == 1 ? JsonResult.getSuccessResult("设置成功") : JsonResult.getErrorResult("设置失败");
            }else if (mmList.isEmpty()){
                jingfeiJiezhishijian.setDanweijiezhiid(UUID.randomUUID().toString());
                jingfeiJiezhishijian.setShifoushezhijiezhi(1l);
                jingfeiJiezhishijian.setJiezhishijian(date);
                int add = jingfeiJiezhishijianService.insertJingfeiJiezhishijian(jingfeiJiezhishijian);
                return add == 1 ? JsonResult.getSuccessResult("设置成功") : JsonResult.getErrorResult("设置失败");
            }else{
                return JsonResult.getErrorResult("设置出错");
            }
        } catch (Exception e) {
            logger.error("jiezhishijian -> shezhi: "+e.getMessage());
            return JsonResult.getErrorResult(e.getMessage());
        }
    }
}
